<!--
 * @Description: 用户留言
 * @Author:  zhangweigang
 * @Date: 2021-02-14 20:08:05
 * @LastEditTime: 2021-04-05 09:38:19
 * @LastEditors: zhangweigang
-->
<template>
  <div>
    <Header></Header>
    <curr-locat title="我要留言"></curr-locat>
    <div class="contain clearfix">
      <div class="right fl" style="width: 20%">
        <ul>
          <li @click="tabClick(2)">
            <img class="lb" src="../../assets/image/xz.png" width="35" alt="查看须知" />
            <p class="lb">查看须知</p>
          </li>
          <li @click="tabClick(0)">
            <img class="lb" src="../../assets/image/ly.png" width="35" alt="我要留言" />
            <p class="lb">我要留言</p>
          </li>
          <li @click="tabClick(1)">
            <img class="lb" src="../../assets/image/ls.png" width="35" alt="查看历史留言" />
            <p class="lb">查看历史留言</p>
          </li>
        </ul>
      </div>
      <div class="fr" style="width: 80%; padding: 10px">
        <!-- 留言须知 -->
        <div class="msg" v-show="xzshow">
          <div class="msg-item">
            <div>
              <h3>
                一、本系统的主要目的用于读者对书籍共享和安全信息的事项进行交流的平台；
              </h3>
            </div>
            <div>
              <h3>二、请留言人遵守以下规定：</h3>
              <ol>
                <li>
                  不得发表违反国家宪法和法律、违反改革开放和四项基本原则的言论；
                </li>
                <li>不得发表造谣、诽谤他人的言论；</li>
                <li>不得发表未经证实的消息；</li>
                <li>不得发表任何形式的广告，企业推广产品或服务；</li>
                <li>请勿发表与个人图书共享系统工作无关的留言；</li>
                <li>请注意语言文明，不要留下无实质意义的内容；</li>
                <li>本栏目将视情况，采取不同方式进行回复；</li>
                <li>
                  留言人承担一切因留言行为而直接或间接引起的法律责任，留言内容的公布不表示本栏目同意留言人所表达的立场和观点；
                </li>
                <li>
                  本栏目拥有发布、保留、删除公众提问的权利，凡不符合本须知规定的留言将予以删除；
                </li>
              </ol>
            </div>
            <div>
              <h3>三、本系统工作人员将会及时回复您的建议及意见。</h3>
            </div>
            <div>
              <h3>四、如在本系统留言，即表明已阅读并接受了上述各项条款。</h3>
            </div>
          </div>
        </div>
        <!-- 我要留言 -->
        <div v-show="lyshow">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="留言主题" prop="subject">
              <el-input type="text" v-model="ruleForm.subject"></el-input>
            </el-form-item>
            <el-form-item label="留言内容" prop="content">
              <el-input type="textarea" :rows="6" v-model="ruleForm.content"></el-input>
            </el-form-item>
            <!-- 后台返回留言日期 <el-form-item label="选择日期" required>
              <el-col :span="11">
                <el-form-item>
                  <el-date-picker type="datetime" placeholder="选择日期" v-model="ruleForm.commentDate" style="width: 100%" @change="changeTimer"></el-date-picker>
                </el-form-item>
              </el-col>
            </el-form-item> -->
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!-- 查看历史留言 -->
        <div v-show="lsshow">
          <div v-if="listData.length > 0">
            <el-table :data="listData" stripe border>
              <el-table-column prop="subject" label="主题名称" header-align="center"></el-table-column>
              <el-table-column label="留言内容" header-align="center">
                <template slot-scope="scope">
                  <div class="content_notice">{{ scope.row.content }}</div>
                </template>
              </el-table-column>
              <el-table-column prop="commentDate" label="留言日期" header-align="center"></el-table-column>
              <el-table-column label="操作" header-align="center">
                <template slot-scope="scope">
                  <!-- 修改按钮  -->
                  <el-button type="warning" size="mini" @click="lookDetailInfo(scope.row)">查看详情</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- <div class="row">
              <div class="title col-1">主题名称</div>
              <div class="title col-2">留言内容</div>
              <div class="title col-3">留言日期</div>
              <div class="title col-4">操作</div>
            </div>
            <div v-for="(item, index) in listData" :key="index">
              <div class="row">
                <div class="col-1">{{ item.subject }}</div>
                <div class="col-2 text_latter">{{ item.content }}</div>
                <div class="col-3">{{ item.commentDate }}</div>
                <div class="col-4">
                  <el-button type="warning" @click="dialogDetailVisible = true">查看详情</el-button>
                </div>
              </div>
            </div> -->
            <!-- 分页 -->
            <div class="pag">
              <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
            </div>
          </div>
          <div class="leave_info" v-else>
            <p>您还未增留言，请尽快去留言！您的留言信息会得到及时反馈！</p>
          </div>
          <!-- 查看详情弹框 -->
          <el-dialog title="留言详情" :visible.sync="dialogDetailVisible" @close="detailClosed">
            <div class="detailMsg">{{ detailText }}</div>
          </el-dialog>
        </div>
        <!-- 查看反馈 -->
        <div v-show="lookMsg"></div>
      </div>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
import { submitLeaveMessage, getLeavePagination, getUserAllInfo } from '../../network/product.js';
export default {
  name: 'LeaveMessage',
  data() {
    return {
      currentPage: 1,
      // 列表总数
      total: 0,
      // 默认分页参数
      pageNum: 1,
      pageSize: 10,
      // 存储当前用户留言数据
      listData: [],
      // 判断左侧栏目显示与隐藏
      lyshow: false,
      xzshow: true,
      lsshow: false,
      lookMsg: false,
      dialogDetailVisible: false,
      detailText: '',
      // 留言提交表单
      ruleForm: {
        subject: '',
        content: '',
        commentDate: '',
        sysRoleId: 0,
        userId: 0,
        username: ''
      },
      rules: {
        subject: [{ required: true, message: '请填留言主题', trigger: 'blur' }],
        content: [{ required: true, message: '请填留言内容', trigger: 'blur' }],
        commentDate: [
          {
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ]
      }
    };
  },
  mounted() {
    // 获取当前用户ID
    this.getCurrentUserId();
    // 获取当前用户分页数据
    this.getPageData(this.pageNum, this.pageSize);
  },
  methods: {
    /**
     * 当前页数的数据发生改变时触发
     * @pageSize 记录每一页显示数据条数
     */
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getPageData(this.pageNum, pageSize);
    },
    /**
     * 当前页显示数据发生改变时触发
     * @pageNum 记录当前页数
     */
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.getPageData(pageNum, this.pageSize);
    },
    /**
     * 留言左侧栏目切换
     * @index 当前栏目索引
     */
    tabClick(index) {
      switch (index) {
        case 0:
          // 查看须知
          this.lyshow = true;
          this.xzshow = false;
          this.lsshow = false;
          this.lookMsg = false;
          break;
        // 查看历史留言
        case 1:
          this.lyshow = false;
          this.xzshow = false;
          this.lsshow = true;
          this.lookMsg = false;
          // 清空后，再获取
          this.listData = [];
          this.getPageData(this.pageNum, this.pageSize);
          break;
        case 2:
          // 我要留言
          this.lyshow = false;
          this.xzshow = true;
          this.lsshow = false;
          this.lookMsg = false;
          break;
        case 3:
          // 我要留言
          this.lyshow = false;
          this.xzshow = false;
          this.lsshow = false;
          this.lookMsg = true;
          break;
        default:
          break;
      }
    },
    // 留言提交
    submitForm(formName) {
      // 获取当前登录用户
      this.ruleForm.username = localStorage.getItem('username');
      this.ruleForm.userId = localStorage.getItem('userId');
      this.$refs[formName].validate((valid) => {
        if (valid) {
          submitLeaveMessage(this.ruleForm).then((res) => {
            if (res.data.code === 0) {
              this.$message({
                showClose: true,
                message: '留言已经成功！请注意查看反馈！',
                type: 'success',
                duration: 4000
              });
              this.$refs[formName].resetFields();
              this.ruleForm.commentDate = '';
            } else {
              this.$message({
                showClose: true,
                message: '留言失败！',
                type: 'error',
                duration: 4000
              });
              return false;
            }
          });
        } else {
          this.$message({
            showClose: true,
            message: '请先输入信息后再留言！',
            type: 'error',
            duration: 4000
          });
          return false;
        }
      });
    },
    // 获取当前用户留言分页数据
    getPageData(pageNum, pageSize) {
      getLeavePagination(pageNum, pageSize).then((res) => {
        if (res.data.code === 0) {
          // 获取留言列表
          this.listData = res.data.rows;
          this.total = res.data.rows.length;
        } else {
          this.$message({
            showClose: true,
            message: '分页数据获取失败，请联系管理员！',
            type: 'error',
            duration: 4000
          });
          return;
        }
      });
    },
    // 获取当前用户Id
    getCurrentUserId() {
      // 获取所用用户信息
      getUserAllInfo().then((res) => {
        if (res.data.code === 0) {
          for (let i = 0; i <= res.data.rows.length - 1; i++) {
            // 得到当前用户ID，并本地存储
            if (res.data.rows[i].username == localStorage.getItem('username')) {
              localStorage.setItem('userId', res.data.rows[i].userId);
              return;
            }
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 查看详情
    lookDetailInfo(scope) {
      this.detailText = scope.content;
      this.dialogDetailVisible = true;
      console.log(scope);
    },
    detailClosed() {
      this.dialogDetailVisible = false;
    }
    //时间选择
    // changeTimer(dateData) {
    //   console.log(dateData, '事件触发');
    //   let date = new Date(dateData);
    //   let y = date.getFullYear();
    //   let m = date.getMonth() + 1;
    //   m = m < 10 ? '0' + m : m;
    //   let d = date.getDate();
    //   d = d < 10 ? '0' + d : d;
    //   let h = date.getHours();
    //   h = h < 10 ? '0' + h : h;
    //   let min = date.getMinutes();
    //   min = min < 10 ? '0' + min : min;
    //   let s = date.getSeconds();
    //   s = s < 10 ? '0' + s : s;
    //   let time = y + '-' + m + '-' + d + ' ' + h + ':' + min + ':' + s;
    //   this.ruleForm.commentDate = time;
    //   return time;
    // },
  }
};
</script>

<style lang="less" scoped>
.contain {
  background-color: #fff;
  .right {
    ul li {
      padding: 10px 5px;
      border-bottom: 1px solid rgb(216, 216, 216);
      &:hover {
        background-color: rgb(243, 243, 243);
        cursor: pointer;
      }
      p {
        vertical-align: middle;
        font-size: 16px;
        padding-left: 5px;
      }
    }
  }
  .msg-item {
    margin: 0px auto;
    width: 94%;
    h3 {
      padding: 5px 0;
      color: #333;
    }
    ol li {
      font-size: 16px;
      color: #333;
      line-height: 30px;
      list-style: inherit;
      padding: 5px 0;
      border-bottom: 1px solid #e4e0e0;
    }
  }
}
.msg {
  padding-bottom: 15px;
  ol {
    padding: 0 0 0 60px;
  }
}
.pag {
  margin-top: 10px;
}
.leave_info {
  text-align: center;
  font-size: 22px;
  font-weight: 800;
  color: rgb(216, 216, 216);
}
.content_notice {
  width: 100%;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
}
.detailMsg {
  font-size: 16px;
}
</style>
